package cn.iocoder.yudao.module.handguard.service.labeltable;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.labeltable.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.labeltable.LabelTableDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.labeltable.LabelTableConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.labeltable.LabelTableMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 标签 Service 实现类
 *
 * @author 超管
 */
@Service
@Validated
public class LabelTableServiceImpl implements LabelTableService {

    @Resource
    private LabelTableMapper labelTableMapper;

    @Override
    public Long createLabelTable(LabelTableCreateReqVO createReqVO) {
        // 插入
        LabelTableDO labelTable = LabelTableConvert.INSTANCE.convert(createReqVO);
        labelTableMapper.insert(labelTable);
        // 返回
        return labelTable.getId();
    }

    @Override
    public void updateLabelTable(LabelTableUpdateReqVO updateReqVO) {
        // 校验存在
        validateLabelTableExists(updateReqVO.getId());
        // 更新
        LabelTableDO updateObj = LabelTableConvert.INSTANCE.convert(updateReqVO);
        labelTableMapper.updateById(updateObj);
    }

    @Override
    public void deleteLabelTable(Long id) {
        // 校验存在
        validateLabelTableExists(id);
        // 删除
        labelTableMapper.deleteById(id);
    }

    private void validateLabelTableExists(Long id) {
        if (labelTableMapper.selectById(id) == null) {
            throw exception(LABEL_TABLE_NOT_EXISTS);
        }
    }

    @Override
    public LabelTableDO getLabelTable(Long id) {
        return labelTableMapper.selectById(id);
    }

    @Override
    public List<LabelTableDO> getLabelTableList() {
        return labelTableMapper.selectList();
    }

    @Override
    public PageResult<LabelTableDO> getLabelTablePage(LabelTablePageReqVO pageReqVO) {
        return labelTableMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LabelTableDO> getLabelTableList(LabelTableExportReqVO exportReqVO) {
        return labelTableMapper.selectList(exportReqVO);
    }

}
